//
//  main.swift
//  ChainOfResponsibility
//
//  Created by LY on 2017/11/6.
//  Copyright © 2017年 DesignPatterns. All rights reserved.
//

import Foundation

let messages:[Message] = [
    Message(from: "bob@example.com", to: "joe@example.com", subject: "Free for lunch?"),
    Message(from: "joe@example.com", to: "alice@acme.com", subject: "New Contracts"),
    Message(from: "pete@example.com", to: "all@example.com", subject: "Priority:All-Hands Meeting")
]

//let localT = localTransmitter()
//let remoteT = remoteTransmitter()
//for msg in messages{
//    if let index = msg.from.index(of: "@"){
//        if msg.to.hasSuffix(String(msg.from[index ..< msg.from.endIndex])){
//            localT.snedMessage(message: msg)
//        }else{
//            remoteT.sendMessage(message: msg)
//        }
//    }else{
//        print("Error: cannot send message to \(msg.from)")
//    }
//}
//使用责任链模式
//if let chain = Transmitter.createChain(){
//    for msg in messages{
//        chain.sendMessage(message: msg)
//    }
//}
//使用工厂方法模式
//if let chain = Transmitter.createChain(localOnly: true){
//    for msg in messages{
//        chain.sendMessage(message: msg)
//    }
//}
//反馈式责任链
//if let chain = Transmitter.createChain(localOnly: true){
//    for msg in messages{
//        let headled = chain.sendMessage(message: msg)
//        print("Message sent : \(headled)")
//    }
//}
//责任链中的其他链
//if let chain = Transmitter.createChain(localOnly: true){
//    for msg in messages{
//        let headled = chain.sendMessage(message: msg, handled: false)
//        print("Message sent : \(headled)")
//    }
//}

